using Microsoft.AspNetCore.Mvc;
using Terra.Core.Common.ApiResponse;
using Terra.Core.Common.ErrorCodes.Constants;

namespace Terra.WebApi.Controllers;

/// <summary>
/// 通用响应格式测试控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
[ApiExplorerSettings(GroupName = "v1")]
public class TestApiResponseController : ControllerBase
{
    /// <summary>
    /// 测试成功响应 - 返回简单数据
    /// </summary>
    /// <returns>字符串消息</returns>
    [HttpGet("success/message")]
    [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status200OK)]
    public IActionResult TestSuccessMessage()
    {
        return Ok(ApiResponse<string>.Success("操作成功"));
    }

    /// <summary>
    /// 测试成功响应 - 返回复杂对象
    /// </summary>
    /// <returns>用户信息</returns>
    [HttpGet("success/object")]
    [ProducesResponseType(typeof(ApiResponse<UserInfo>), StatusCodes.Status200OK)]
    public IActionResult TestSuccessObject()
    {
        var user = new UserInfo
        {
            Id = 1,
            Username = "test_user",
            Email = "test@example.com",
            Roles = new[] { "admin", "user" }
        };
        return Ok(ApiResponse<UserInfo>.Success(user, "获取用户信息成功"));
    }

    /// <summary>
    /// 测试成功响应 - 返回集合数据
    /// </summary>
    /// <returns>用户列表</returns>
    [HttpGet("success/list")]
    [ProducesResponseType(typeof(ApiResponse<List<UserInfo>>), StatusCodes.Status200OK)]
    public IActionResult TestSuccessList()
    {
        var users = new List<UserInfo>
        {
            new() { Id = 1, Username = "user1", Email = "user1@example.com", Roles = new[] { "user" } },
            new() { Id = 2, Username = "user2", Email = "user2@example.com", Roles = new[] { "admin" } }
        };
        return Ok(ApiResponse<List<UserInfo>>.Success(users, "获取用户列表成功"));
    }

    /// <summary>
    /// 测试成功响应 - 返回分页数据
    /// </summary>
    /// <returns>分页用户列表</returns>
    [HttpGet("success/paged")]
    [ProducesResponseType(typeof(ApiResponse<PagedResult<UserInfo>>), StatusCodes.Status200OK)]
    public IActionResult TestSuccessPaged()
    {
        var users = new List<UserInfo>
        {
            new() { Id = 1, Username = "user1", Email = "user1@example.com", Roles = new[] { "user" } },
            new() { Id = 2, Username = "user2", Email = "user2@example.com", Roles = new[] { "admin" } }
        };

        var result = new PagedResult<UserInfo>
        {
            Items = users,
            TotalCount = 100,
            PageIndex = 1,
            PageSize = 10
        };

        return Ok(ApiResponse<PagedResult<UserInfo>>.Success(result, "获取分页数据成功"));
    }

    /// <summary>
    /// 测试失败响应 - 自定义错误码
    /// </summary>
    /// <returns>错误信息</returns>
    [HttpGet("fail/custom")]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
    public IActionResult TestFailCustom()
    {
        return BadRequest(ApiResponse.Error(BusinessErrorCodes.BusinessError, "自定义错误信息"));
    }

    /// <summary>
    /// 测试空数据响应
    /// </summary>
    /// <returns>空数据响应</returns>
    [HttpGet("success/empty")]
    [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)]
    public IActionResult TestSuccessEmpty()
    {
        return Ok(ApiResponse.Success("空数据响应成功"));
    }
}

/// <summary>
/// 用户信息
/// </summary>
public class UserInfo
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty;

    /// <summary>
    /// 电子邮箱
    /// </summary>
    public string Email { get; set; } = string.Empty;

    /// <summary>
    /// 角色列表
    /// </summary>
    public string[] Roles { get; set; } = Array.Empty<string>();
}

/// <summary>
/// 分页结果
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class PagedResult<T>
{
    /// <summary>
    /// 当前页数据
    /// </summary>
    public List<T> Items { get; set; } = new();

    /// <summary>
    /// 总记录数
    /// </summary>
    public int TotalCount { get; set; }

    /// <summary>
    /// 当前页码
    /// </summary>
    public int PageIndex { get; set; }

    /// <summary>
    /// 每页大小
    /// </summary>
    public int PageSize { get; set; }

    /// <summary>
    /// 总页数
    /// </summary>
    public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);

    /// <summary>
    /// 是否有上一页
    /// </summary>
    public bool HasPreviousPage => PageIndex > 1;

    /// <summary>
    /// 是否有下一页
    /// </summary>
    public bool HasNextPage => PageIndex < TotalPages;
}